package org.jeecg.modules.ncmeapis.processTest.processDefinition.holiday;

import ncme.digital.hszy.common.utils.DateUtils;
import ncme.digital.hszy.ep.ProcessDefinition;
import ncme.digital.hszy.ep.ProcessNode;
import ncme.digital.hszy.ep.base.SubProcessDefinition;
import ncme.digital.hszy.ep.db.entity.NodeDataMap;
import ncme.digital.hszy.ep.interfaces.EndListener;
import ncme.digital.hszy.ep.pre.EndNode;
import ncme.digital.hszy.ep.pre.MergeNode;
import ncme.digital.hszy.ep.pre.StartNode;
import org.jeecg.modules.ncmeapis.processTest.processDefinition.commonNodes.DeanApproveNode;
import org.jeecg.modules.ncmeapis.processTest.processDefinition.commonNodes.DirectorApproveNode;
import org.jeecg.modules.ncmeapis.processTest.processDefinition.commonNodes.RecordEventNode;
import org.jeecg.modules.ncmeapis.processTest.processDefinition.holiday.nodes.ApplyHolidayNode;
import org.jeecg.modules.ncmeapis.processTest.business.entity.HolidayForm;

//@Component
public class HolidayProcess extends ProcessDefinition {

    @Override
    public String description(StartNode processNode) {
        return "请假申请流程";
    }

    @Override
    public EndNode define(StartNode start) throws Exception {
        //请假申请
        ApplyHolidayNode applyHolidayNode = start.link(ApplyHolidayNode.class);
        //分支
        MergeNode mergeNode = applyHolidayNode.branch(new SubProcessDefinition<ApplyHolidayNode>() {
            @Override
            public boolean judge(ApplyHolidayNode processNode) {
                HolidayForm holidayForm = processNode.getHolidayForm();
                return DateUtils.getDateDiff(holidayForm.getEndDate(), holidayForm.getBeginDate()) < 3;
            }

            @Override
            public EndNode define(ApplyHolidayNode start) throws Exception {
                //所长审批
                return start.link(DirectorApproveNode.class).end();
            }
        }, new SubProcessDefinition<ApplyHolidayNode>() {
            @Override
            public boolean judge(ApplyHolidayNode processNode) {
                return true;
            }

            @Override
            public EndNode define(ApplyHolidayNode start) throws Exception {
                //院长审批
                return start.link(DeanApproveNode.class).end();
            }
        });
        //记录请假单（自动存档并结束）
        RecordEventNode recordHolidayNode = mergeNode.link(RecordEventNode.class);
        //结束
        return recordHolidayNode.end(new EndListener() {
            @Override
            public void handle(NodeDataMap<Class<?>, ProcessNode<?>> nodesMap) {
                System.out.println(nodesMap);
            }
        });
    }

}
